package com.pdg.mcplugin.invisiblehand.commandprocessors;

import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.inventory.ItemStack;

import com.pdg.mcplugin.common.ArgumentList;
import com.pdg.mcplugin.common.baseclasses.CommandProcessorBase;
import com.pdg.mcplugin.invisiblehand.InvisibleHand;
import com.pdg.mcplugin.invisiblehand.dataproviders.Commodity;
import com.pdg.mcplugin.invisiblehand.dataproviders.CommodityKey;

public class CreateCommandProcessor extends CommandProcessorBase<InvisibleHand> {

	private static final String messageCreatedEntry = "[IH] Created commodity '%s' for world '%s'.";
	private static final String errorCreatedEntry = "[IH] Could not create commodity '%s' for world '%s'. Likely it already exists.";
	private static final String messageNotEnoughArguments = "[IH] Not enough arguments to create commodity!";
	private static final int argumentCount = 12;

	public CreateCommandProcessor(InvisibleHand thePlugin) {
		super(thePlugin, new String[]{
				"/ih create - help with the create command",
				"/ih create (commodity-name) (material-id) (material-data) (base-price) (base-supply) (base-demand) (supply-factor) (demand-factor) (mark-up) (sales-count) (purchase-count) (stock) (enabled) - creates a commodity with the given values for the current world",
				"/ih create (commodity-name) (material-id) (material-data) (base-price) (base-supply) (base-demand) (supply-factor) (demand-factor) (mark-up) (sales-count) (purchase-count) (stock) (enabled) (world name) - creates a commodity with the given values in (world name)",
		});
	}

	@Override
	protected boolean specialProcessCommand(CommandSender theCommandSender,
			String theSubCommand, ArgumentList theArgumentList) {
		String theWorldName = Utilities.getWorldIfPlayer(theCommandSender);
		if(!Utilities.checkArgumentCount(theCommandSender,theWorldName,theArgumentList,argumentCount,messageNotEnoughArguments)){
			return false;
		}
		String theCommodityName = theSubCommand;
		Integer theMaterialId = Integer.parseInt(theArgumentList.pop());
		Byte theMaterialData = Byte.parseByte(theArgumentList.pop());
		Double theBasePrice = Double.parseDouble(theArgumentList.pop());
		Double theBaseSupply = Double.parseDouble(theArgumentList.pop());
		Double theBaseDemand = Double.parseDouble(theArgumentList.pop());
		Double theSupplyFactor = Double.parseDouble(theArgumentList.pop());
		Double theDemandFactor = Double.parseDouble(theArgumentList.pop());
		Double theMarkUp = Double.parseDouble(theArgumentList.pop());
		Integer theSalesCount = Integer.parseInt(theArgumentList.pop());
		Integer thePurchaseCount = Integer.parseInt(theArgumentList.pop());
		Integer theStock = Integer.parseInt(theArgumentList.pop());
		Boolean theEnabled = Boolean.parseBoolean(theArgumentList.pop());
		if(theWorldName==null){
			theWorldName = Utilities.getWorldName(theArgumentList);
		}
		CommodityKey theKey = new CommodityKey(theCommodityName, theWorldName);
		Commodity theCommodity = new Commodity(theCommodityName, theWorldName, new ItemStack(Material.getMaterial(theMaterialId), 1, theMaterialData), theBasePrice , theBaseSupply , theBaseDemand , theSupplyFactor , theDemandFactor , theMarkUp , theSalesCount , thePurchaseCount , theStock , theEnabled);
		if(getPlugin().getDataProvider().getCommodityTableProvider().create(theKey, theCommodity)){
			theCommandSender.sendMessage(String.format(messageCreatedEntry,theCommodityName,theWorldName));
			return true;
		}
		else{
			theCommandSender.sendMessage(String.format(errorCreatedEntry,theCommodityName,theWorldName));
			return true;
		}
	}

}
